<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品分类</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片视图 -->
<el-card>
  <el-row>
    <el-col>
      <el-button type="primary" @click="addcatdialog">
        添加分类
      </el-button>
    </el-col>
  </el-row>
  <!-- 树形表格 复选框election-type，展开行效果expand-type，show-index索引号， border纵向边框，show-row-hover鼠标覆盖上来高亮效果-->
  <tree-table class="treetable"
   :data="catelist"
   :columns="columns"
   :selection-type="false"
   :expand-type="false"
   show-index
   border
   :show-row-hover="false">
   <!-- 是否有效 -->
   <template slot="isok" slot-scope="scope">
     <i style="color:lightgreen" v-if="scope.row.cat_detected" class="el-icon-success"> </i>
     <i style="color:red" v-if="!scope.row.cat_detected" class="el-icon-error"></i>
   </template>
   <!-- 等级排序 -->
   <template slot="sort" slot-scope="scope">
     <el-tag v-if="scope.row.cat_level==0" type="success">一级</el-tag>
     <el-tag v-if="scope.row.cat_level==1" type="warning">二级</el-tag>
     <el-tag v-if="scope.row.cat_level==2" type="danger">三级</el-tag>
   </template>
   <!-- 操作 -->
   <template slot="opt" slot-scope="">
     <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
<el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
   </template>
   </tree-table>
  <!-- 分页 -->
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="queryParams.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</el-card>
<!-- 添加商品类型对话框 -->
<el-dialog
  title="添加商品类型"
  :visible.sync="addcatdialogVisible"
  width="50%"
  @close="resetaddcatForm">
  <el-form :model="addcatForm" :rules="addcatFormRules" ref="addcatFormRef" label-width="100px">
  <el-form-item label="商品分类" prop="cat_name">
    <el-input v-model="addcatForm.cat_name"></el-input>
  </el-form-item>
  <el-form-item label="父级分类名称">
    <!-- options指定数据源 -->
    <!-- props指定级联选择器的配置对象 -->
    <!-- v-model备份选中得id数组 -->
    <!-- 默认清空选项clearable -->
    <!-- change-on-select 是否选中任意一级的分类  已失效，props的checkStrictly: true代替-->
    <el-cascader
    v-model="selectedkeys"
    :options="parentcatlist"
    :props="cascaderprops"
    @change="handleChange"
    clearable></el-cascader>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addcatdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addcat">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      catelist: [],
      queryParams: {
        // type: '3',
        pagesize: 5,
        pagenum: 1
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        // 自定义一个模板列
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      // 添加分类
      addcatdialogVisible: false,
      addcatForm: {
        cat_name: '',
        // 父节点id
        cat_pid: '',
        // 分类等级一级
        cat_level: 0
      },
      addcatFormRules: {
        cat_name: [{ required: true, message: '请输入商品分类名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }]
      },
      // 父级分组列表
      parentcatlist: [],
      // 级联选择器的配置对象
      cascaderprops: {
        // 看到的属性
        label: 'cat_name',
        // 选中得值的属性
        value: 'cat_id',
        // 父子嵌套的属性
        children: 'children',
        expandTrigger: 'hover',
        // 可选任意一级的分类
        checkStrictly: true
      },
      selectedkeys: []
    }
  },
  created () {
    this.getcatelist()
  },
  methods: {
    // 获取分类列表数据
    async getcatelist () {
      const { data: res } = await this.$http.get('categories', { params: this.queryParams })
      // console.log(res)
      // if (res.meta.status !== 200) return this.$message.erro(res.meta.msg)
      this.catelist = res.data.result
      this.total = res.data.total
    },
    // 分页
    handleSizeChange (val) {
      this.queryParams.pagesize = val
      this.getcatelist()
    },
    handleCurrentChange (val) {
      this.queryParams.pagenum = val
      this.getcatelist()
    },
    // 添加商品分类
    addcatdialog () {
      this.getparentcatlist()
      this.addcatdialogVisible = true
      // console.log('123')
    },
    addcat () {
      this.$refs.addcatFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addcatForm)
        console.log(res)
        if (res.meta.status !== 201) return
        this.addcatdialogVisible = false
        this.getcatelist()
      })
    },
    resetaddcatForm () {
      this.addcatForm = {}
      this.selectedkeys = []
    },
    // 获取父级商品分类列表
    async getparentcatlist () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.erro(res.meta.msg)
      this.parentcatlist = res.data
    },
    // 级联选择项发生数据时触发
    handleChange () {
      // 判断是否选择了分类
      console.log(this.selectedkeys)
      if (this.selectedkeys.length > 0) {
        // eslint-disable-next-line no-unused-expressions
        this.addcatForm.cat_pid = this.selectedkeys[this.selectedkeys.length - 1]
        this.addcatForm.cat_level = this.selectedkeys.length
      } else {
        this.addcatForm.cat_id = 0
        this.addcatForm.cat_level = 0
      }
    }

  }
}
</script>
<style scoped>
.treetable{
  margin-top: 15px;
}

</style>
